package com.dyoud.api.shop.service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.douyoudian.utils.DateUtils;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dyoud.api.shop.shopInfo.dto.AddShopInfoDto;
import com.dyoud.api.shop.shopInfo.dto.ShopPageQueryDto;
import com.dyoud.api.shop.shopInfo.dto.UpdateShopInfoDto;
import com.dyoud.common.api.model.City;
import com.dyoud.common.api.service.CityService;
import com.dyoud.core.DozerUtils;
import com.dyoud.partner.api.model.Partner;
import com.dyoud.partner.api.service.PartnerService;
import com.dyoud.shop.api.enums.StateEnum;
import com.dyoud.shop.api.model.ShopInCome;
import com.dyoud.shop.api.model.ShopInfo;
import com.dyoud.shop.api.model.ShopPic;
import com.dyoud.shop.api.model.ShopType;
import com.dyoud.shop.api.query.ShopPageQuery;
import com.dyoud.shop.api.request.ShopInComeRequest;
import com.dyoud.shop.api.request.ShopInfoRequest;
import com.dyoud.shop.api.response.CalcUserIncomeInfoResponse;
import com.dyoud.shop.api.service.ShopInComeService;
import com.dyoud.shop.api.service.ShopInfoService;
import com.dyoud.shop.api.service.ShopPicService;
import com.dyoud.shop.api.service.ShopTypeService;
import com.dyoud.user.api.service.UserIncomeService;
import com.github.pagehelper.PageInfo;

/***
 * 
 * @author wangshuaifei
 * @date 2018/03/02
 */
@Service
public class ShopApiService {

    @Reference
    private ShopInfoService shopInfoService;

    @Reference
    private CityService cityService;

    @Reference
    private ShopInComeService shopInComeService;

    @Reference
    private ShopTypeService shopTypeService;
    
    @Reference
    private PartnerService partnerService;
    
    @Reference
    private UserIncomeService userIncomeService;
    
    @Reference
    private ShopPicService shopPicService;

    /**
     * 添加店铺
     * 
     * @param request
     * @return
     */
    public boolean addShopInfo(AddShopInfoDto shopInfoDto) {
        // TODO Auto-generated method stub
        ShopInfoRequest shopinfo = new ShopInfoRequest();
        DozerUtils.copy(shopInfoDto, shopinfo);
        return shopInfoService.addShopInfo(shopinfo) > 0;
    }

    /**
     * 刪除店铺,只是修改店铺的禁用状态
     * 
     * @param shopId
     * @return
     */
    public int delShopInfo(Long shopId) {
        // TODO Auto-generated method stub
        return shopInfoService.delShopInfo(shopId);
    }

    /**
     * 修改店铺
     * 
     * @param shopId
     * @return
     */
    public boolean updateShopInfo(UpdateShopInfoDto dto) {
        ShopInfoRequest shopinfo = new ShopInfoRequest();
        DozerUtils.copy(dto, shopinfo);
        return shopInfoService.updateShopInfo(shopinfo) > 0;
    }

    /***
     * 根据店铺id查询单个店铺信息
     * 
     * @param id
     * @return
     */
    public ShopInfo getByShopId(Long id){
        return shopInfoService.getByShopId(id);
    }
    /***
     * 根据店铺id查询单个店铺信息
     * 
     * @param id
     * @return
     */
    public Map<String, Object> getByShopInfo(Integer pageNo, Integer pageSize, Long id) {
        //店铺图片
        List<ShopPic> shopPicList = shopPicService.selectByShopIdAndType(id, StateEnum.FINANCEPIC.getValue());
        //店铺财务信息图片
        List<ShopPic> financePicList = shopPicService.selectByShopIdAndType(id, StateEnum.FINANCEPIC.getValue());
        //根据店铺id查询已送出比例与成本
        Map<String, BigDecimal> shopResult = userIncomeService.getSumIncomeScaleAndComeValue(id);
      //店铺详细信息
        ShopInfo shopinfo = shopInfoService.getByShopId(id);
        Partner partner = partnerService.selectByPrimaryKey(shopinfo.getPartnerId());
        BigDecimal countScale = new BigDecimal("0");
        BigDecimal sum = new BigDecimal("0");
        //店铺剩余比例
        BigDecimal residueCountScale = new BigDecimal("0");
      //店铺收益是否已送完
        Integer isOk = 0;
        if(shopResult != null) {
          //店铺共计已送出收益
            countScale = shopResult.get("income_scale");
            
            residueCountScale = (new BigDecimal("100")).subtract(countScale);
          //店铺共计已送出成本
            sum = shopResult.get("income_value");
            
          //店铺收益是否赠送完毕
            if(countScale.divide(new BigDecimal("100")).compareTo(new BigDecimal("1")) >= 0) {
                isOk = 1;
                residueCountScale = new BigDecimal("0");
            }
        }
        
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.set(Calendar.DATE, now.get(Calendar.DATE)-1);
        System.out.println("当前处理的时间是："+DateUtils.dateToStr(now.getTime()));
        //根据店铺id查询--昨日--的已送出比例与成本
        Map<String, BigDecimal> yesTerDay = userIncomeService.getSumInComeScaleAndComeValueByYesterday(id, DateUtils.dateToStr(now.getTime()));
        now.clear();
        now.setTime(DateUtils.getNow());
        if(now.get(Calendar.MONTH) > 0) {
            now.set(Calendar.MONTH, now.get(Calendar.MONTH)-1);
        } else {
            now.set(Calendar.MONTH, 11);
        }
        DateUtils.pattern="yyyy-MM";
        System.out.println("当前处理的时间是："+DateUtils.dateToStr(now.getTime()));
      //根据店铺id查询--上月--的已送出比例与成本
        Map<String, BigDecimal> lastMonth = userIncomeService.getSumInComeScaleAndComeValueByMonth(id, DateUtils.dateToStr(now.getTime()));
        ShopPageQuery query = new ShopPageQuery();
        query.setIsDel(StateEnum.NOTDEL.getValue());
        query.setState(StateEnum.DISABLE.getValue());
        query.setPageNo(pageNo);
        query.setPageSize(pageSize);
        //查询全部筹备中商家
        //PageInfo<Map<String, Object>> shopInfoList = shopInfoService.selectByStateAndIsDel(query);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("shopinfo", shopinfo);
        resultMap.put("shopPic", shopPicList);
        resultMap.put("financePic", financePicList);
        resultMap.put("countScale", countScale);
        resultMap.put("sum", sum);
        resultMap.put("isOk", isOk);
        resultMap.put("residueCountScale", residueCountScale);
        resultMap.put("phone", partner.getPhone());
        if(lastMonth != null) {
            resultMap.put("lastMonth", lastMonth.get("income_value"));
        }else {
            resultMap.put("lastMonth", null);
        }
        if(yesTerDay != null) {
            resultMap.put("yesTerDay", yesTerDay.get("income_value"));
        }else {
            resultMap.put("yesTerDay", null);
        }
        
        //resultMap.put("shopInfoList", shopInfoList);
        
        return resultMap;
    }

    /**
     * 分页查询店铺
     * 
     * @param shopId
     * @return
     */
    public PageInfo<ShopInfo> selectByNameAndState(ShopPageQueryDto dto) {
        ShopPageQuery query = new ShopPageQuery();
        DozerUtils.copy(dto, query);
        return shopInfoService.selectByNameAndState(query);
    }

    /**
     * 根据cityNo 市，县
     * 
     * @param cityNo
     * @return
     */
    public List<City> selectByCityNo(Long cityNo) {
        return cityService.selectByCityNo(cityNo);
    }

    /**
     * 获取所有省份
     * 
     * @param cityNo
     * @return
     */
    public List<City> selectProvince() {
        return cityService.selectProvince();
    }

    /**
     * 分页查询店铺，根据店铺名称,店铺状态,店铺所在城市
     * 
     * @param query
     * 
     * @return
     */
    public PageInfo<ShopInfo> selectByNameAndStateAndCity(ShopPageQueryDto dto) {
        ShopPageQuery query = new ShopPageQuery();
        DozerUtils.copy(dto, query);
        // 没时间加方法，就先这样
        query.setIsDel(0);
        PageInfo<ShopInfo> pages = shopInfoService.selectByNameAndStateAndCityAndTypeIdAndShopType(query);
        return pages;
    }

    public List<ShopInCome> selectList(ShopInComeRequest request) {
        return shopInComeService.selectList(request);
    }

    public int deleteByPrimaryKey(Long id) {
        return shopInComeService.deleteByPrimaryKey(id);
    }

    public int insert(ShopInCome model) {

        return shopInComeService.insert(model);
    }

    public ShopInCome selectByPrimaryKey(Long id) {
        return shopInComeService.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKey(ShopInCome shopInCome) {
        return shopInComeService.updateByPrimaryKey(shopInCome);
    }

    /***
     * 查询所有的店铺类型
     * 
     * @return
     */
    public List<ShopType> selectShopTypeAll() {
        return shopTypeService.selectAll();
    }
    /***
     * 显示到操作员管理员主页
     * @param partnerId
     * @param shopId
     * @return
     */
    public Map<String,BigDecimal> appDisplayHomeAdmin(Long partnerId, Long  shopId) {
        Partner partner = partnerService.selectByPrimaryKey(partnerId);
        BigDecimal sum = userIncomeService.getSumInComeScaleByShopId(shopId);
        Map<String,BigDecimal> json = new HashMap<String,BigDecimal>();
        json.put("advancePayMoney", partner.getAdvancePayMoney());
        json.put("depositMoney", partner.getDepositMoney());
        json.put("sum", sum);
        return json;
    }
    
    /**
     * 给予用户收益权预览页面
     * 
     * @param shopId 店铺ID
     * @param phone 用户手机号
     * @param spendingMoney 投资金额(消费多少)
     * @return
     */
    public CalcUserIncomeInfoResponse incomeToUserPre(Long shopId, String phone, BigDecimal spendingMoney) {
        return shopInfoService.incomeToUserPre(shopId, phone, spendingMoney);
    }

}
